package com.bob.common.utils;

import com.bob.common.DataStore;
import com.bob.common.bean.PomBaseEntity;
import com.bob.common.bean.PomEntity;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

public class PomUtil {

    /**
     * 生成finalName  {artifactId}-${version}
     * @param artifactId 不能为空
     * @param version 不能为空
     * @return artifactId-version
     */
    public static  String finalName(@NotNull String artifactId, @NotNull String version){
        return artifactId + "-" +version;
    }


    public static boolean isProperties(String version){
        return StringUtils.isNotBlank(version ) && version.startsWith("${") && version.endsWith("}");
    }

    /**
     * 在pom是否存在artifactId依赖
     * @param pomEntity 不能为空
     * @param artifactId 不能为空
     */
    public static PomBaseEntity existDependency(PomEntity pomEntity, String artifactId){
        if(pomEntity==null || StringUtils.isBlank(artifactId)){
            return null;
        }
        for (PomBaseEntity p: pomEntity.getDependencies()) {
            if (p.getArtifactId().equals(artifactId)){
                return p;
            }
        }

        for (PomBaseEntity p: pomEntity.getDependencies()) {
            PomBaseEntity exist = existDependency(DataStore.getDataStore().getPomEntityMap().get(p.getArtifactId()), artifactId);
            if (exist !=null){
                return exist;
            }
        }
        return null;
    }
}
